﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.OleDb;
using System.Data;
using GuoShuCan.SDK.Web.ServerControls.Manage;
using GuoShuCan.SDK.Web.ServerControls;
using GuoShuCan.SDK.Web;
using GuoShuCan.SDK.Web.Globalization;
/// <summary>
/// 
/// </summary>
public partial class Manage_Add : UserControlBase, ILoadMenuID
{
    #region ILoadMenuID
    string ILoadMenuID.MenuID
    {
        get
        {
            return "Manage_Add";
        }
    }
    #endregion
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void PictureButtonOperation1_Click(object sender, ImageClickEventArgs e)
    {
        this.PhotoError.Text = string.Empty;
        this.NameError.Text = string.Empty;
        string name = this.Name.Text.Trim();
        string description = Description.Text.Trim();
        //图片文件。
        string _photoUrl = string.Empty;
        //验证图片。
        if (this.Photo.PostedFile != null && this.Photo.PostedFile.ContentLength > 0)
        {
            int upFileSize = this.Photo.PostedFile.ContentLength;
            this.PhotoError.Text = (upFileSize / 1024).ToString() + " KB";
            string extFileString = System.IO.Path.GetExtension(this.Photo.PostedFile.FileName).ToLower();
            if (this.ScriptManager.IsInAsyncPostBack)
            {
                if (!(extFileString == ".jpg" || extFileString == ".png" || extFileString == ".bmp" || extFileString == ".gif"))
                {
                    this.PhotoError.Text = "<br />" + this.GetLocalResourceObject("PhotoExtensionError").ToString();
                    base.UpdateComplete();
                    return;
                }
            }
            string saveFileName = Guid.NewGuid().ToString().Replace("-", "").ToLower();
            //生成文件。
            this.Photo.PostedFile.SaveAs(Server.MapPath("..\\Upload\\" + saveFileName + extFileString));
            _photoUrl = saveFileName + extFileString;
        }
        //验证型号。
        if (name == string.Empty)
        {
            DeletePhoto(_photoUrl);
            this.Name.Focus();
            this.NameError.Text = "<br />" + this.GetLocalResourceObject("NameNullError").ToString();
            base.UpdateComplete();
            return;
        }
        using (OleDbConnection conn = new OleDbConnection(DataConfig.ConnectionString))
        {
            using (OleDbCommand comm = conn.CreateCommand())
            {
                try
                {
                    comm.CommandType = CommandType.Text;
                    //验证型号重复。
                    switch (this.ILanguage.Language)
                    {
                        case "zh-CN":
                            comm.CommandText = "SELECT Name FROM CNList WHERE (Name = @Name)";
                            break;
                        case "zh-TW":
                            comm.CommandText = "SELECT Name FROM TWList WHERE (Name = @Name)";
                            break;
                        case "en-US":
                            comm.CommandText = "SELECT Name FROM USList WHERE (Name = @Name)";
                            break;
                        case "ja-JP":
                            comm.CommandText = "SELECT Name FROM JPList WHERE (Name = @Name)";
                            break;
                        case "ko-KR":
                            comm.CommandText = "SELECT Name FROM KRList WHERE (Name = @Name)";
                            break;
                    }
                    comm.Parameters.Add("@Name", OleDbType.VarChar, 255).Value = name;
                    conn.Open();
                    object o = comm.ExecuteScalar();
                    if (o != null)
                    {
                        conn.Close();
                        DeletePhoto(_photoUrl);
                        this.NameError.Text = "<br />" + this.GetLocalResourceObject("NameRepeatError").ToString();
                        base.UpdateComplete();
                        return;
                    }
                    comm.Parameters.Clear();
                    switch (this.ILanguage.Language)
                    {
                        case "zh-CN":
                            comm.CommandText = "INSERT INTO CNList (Name, Photo, Description, CreateTime) VALUES (@Name, @Photo, @Description, @CreateTime)";
                            break;
                        case "zh-TW":
                            comm.CommandText = "INSERT INTO TWList (Name, Photo, Description, CreateTime) VALUES (@Name, @Photo, @Description, @CreateTime)";
                            break;
                        case "en-US":
                            comm.CommandText = "INSERT INTO USList (Name, Photo, Description, CreateTime) VALUES (@Name, @Photo, @Description, @CreateTime)";
                            break;
                        case "ja-JP":
                            comm.CommandText = "INSERT INTO JPList (Name, Photo, Description, CreateTime) VALUES (@Name, @Photo, @Description, @CreateTime)";
                            break;
                        case "ko-KR":
                            comm.CommandText = "INSERT INTO KRList (Name, Photo, Description, CreateTime) VALUES (@Name, @Photo, @Description, @CreateTime)";
                            break;
                    }
                    comm.Parameters.Add("@Name", OleDbType.VarWChar, 255).Value = name;
                    comm.Parameters.Add("@Photo", OleDbType.VarWChar, 36).Value = _photoUrl;
                    comm.Parameters.Add("@Description", OleDbType.VarWChar, description.Length).Value = description;
                    comm.Parameters.Add("@CreateTime", OleDbType.DBDate, 8).Value = DateTime.Now;
                    if (comm.ExecuteNonQuery() != 1)
                    {
                        conn.Close();
                        DeletePhoto(_photoUrl);
                        throw new ArgumentException();
                    }
                }
                catch (Exception ex)
                {
                    conn.Close();
                    this.ScriptManager.AsyncPostBackErrorMessage = ex.Message;
                    DeletePhoto(_photoUrl);
                }
            }
        }
        base.UpdateComplete();

    }
    /// <summary>
    /// 删除图片
    /// </summary>
    /// <param name="photoUrl"></param>
    void DeletePhoto(string photoUrl)
    {
        if (photoUrl != string.Empty)
        {
            //存在旧的图片删除它。
            try
            {
                //创建删除图片路径。
                string deleteLocalPhoto = this.Server.MapPath("~/Upload/" + photoUrl);
                //验证图片是否存在磁盘。
                if (System.IO.File.Exists(deleteLocalPhoto))
                {
                    //图片存在删除它。
                    System.IO.File.Delete(deleteLocalPhoto);
                }
            }
            catch (Exception) { }
        }
    }
    protected override void OnInit(EventArgs e)
    {
        base.OnInit(e);
        base.RegisterAsyncListControl(this.PictureButtonOperation1);
    }
    /// <summary>
    /// 更新区域数据。
    /// </summary>
    protected override void OnUpdateCulture()
    {
        ((GuoShuCan.SDK.Web.Globalization.IUpdateCulture)this.PictureButtonOperation1).UpdateCulture();
    }
    /// <summary>
    /// 
    /// </summary>
    protected override void OnUpdateTitle()
    {
        this.Page.Title = ((IGetLocalResourceObject)this.Page).GetLocalResourceObject("Title").ToString() + (" - [ " + this.GetLocalResourceObject("Title").ToString() + " ]");
    }
}